﻿using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharedKernel.Infrastructure.Comm.Helpers
{
    /// <summary>
    /// 图形验证码帮助类
    /// </summary>
    public class VerifyCodeHelper
    {

        /// <summary>
        /// 生成随机验证码数字 
        /// </summary>
        /// <param name="codelen">验证码长度</param>
        /// <returns>返回验证码</returns>
        public static string CreateCodeNumber(int codelen)
        {
            if (codelen < 1)
            {
                return string.Empty;
            }
            int number;
            StringBuilder strCheckCode = new StringBuilder();
            Random random = new Random();
            for (int index = 0; index < codelen; index++)
            {
                number = random.Next();
                strCheckCode.Append((char)('0' + (char)(number % 10)));//生成随机数字
            }
            return strCheckCode.ToString();
        }

        /// <summary>
        /// 生成随机验证码数字+字母
        /// </summary>
        /// <param name="codelen">验证码长度</param>
        /// <returns>返回验证码</returns>
        public static string CreateCode(int codelen)
        {
            if (codelen < 1)
            {
                return string.Empty;
            }
            int number;
            StringBuilder strCheckCode = new StringBuilder();
            Random random = new Random();
            for (int index = 0; index < codelen; index++)
            {
                number = random.Next();
                if (number % 2 == 0)
                {
                    strCheckCode.Append((char)('0' + (char)(number % 10)));//生成随机数字
                }
                else
                {
                    strCheckCode.Append((char)('A' + (char)(number % 26)));//生成随机字母
                }
            }
            return strCheckCode.ToString();
        }
         
    }
}
